package com.ft.turorial.spring.boot;

import java.util.Properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

@SpringBootApplication
@ServletComponentScan
public class SpringBoot15ShiroRegistrationbeanApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBoot15ShiroRegistrationbeanApplication.class, args);
	}
	
	/**
     * Spring异常处理器:例如权限异常(403)、账号被锁定、内部服务器错误(500)、访问资源部存在(404)等
     * 
     * 其中404错误，需要单独处理，定义一个Controller 集成 ErrorController 接口
     * @return
     */
    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
    	SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
    	Properties mappings = new Properties();
    	mappings.put("org.apache.shiro.authz.UnauthorizedException", "pages/403");
    	mappings.put("org.apache.shiro.authc.LockedAccountException", "pages/locked");
    	mappings.put("java.lang.Throwable", "pages/500");
		simpleMappingExceptionResolver.setExceptionMappings(mappings);
    	return simpleMappingExceptionResolver;
    }
}
